package com.hd.trans.ui.service;

import android.util.Log;


import com.hd.trans.common.Constants;
import com.hd.trans.network.bean.ConfigResponse;
import com.hd.trans.network.bean.MyHttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.observers.DisposableObserver;
import retrofit2.HttpException;

/**
 * @作者 yang
 * @日期 2018/10/8 16:08
 * @描述 网络请求回调
 */
public class NetDisposableObserver2<T extends ConfigResponse> extends DisposableObserver<T> {

    private static final String TAG = "NetDisposableObserver";

    private NetCallBack<T> netCallBack;

    public interface NetCallBack<T2> {
        void onResponse(T2 response);

        void onError(int code, String msg);

        void onTokenInvalid(String msg);
    }


    public NetDisposableObserver2(NetCallBack<T> netCallBack) {
        this.netCallBack = netCallBack;
    }

    @Override
    public void onNext(T tDataResponse) {
        try {
            if (tDataResponse != null) {
                netCallBack.onResponse(tDataResponse);
            } else {
                netCallBack.onError(500, "数据解析失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable e) {
        try {
            if (e instanceof SocketTimeoutException) {
                netCallBack.onError(Constants.ResponseCode.ERROR_NETWORK, "请求超时");
            } else if (e instanceof ConnectException) {
                netCallBack.onError(Constants.ResponseCode.ERROR_NETWORK, "网络连接超时");
            } else if (e instanceof SSLHandshakeException) {
                netCallBack.onError(500, "安全证书异常");
            } else if (e instanceof MyHttpException) {
                MyHttpException myHttpException = (MyHttpException) e;
                netCallBack.onError(myHttpException.code(), myHttpException.message());
            } else if (e instanceof HttpException) {
                boolean isMyHttpExc = false;
                try {
                    MyHttpException myHttpException2 = new MyHttpException(((HttpException) e).response());
                    netCallBack.onError(myHttpException2.code(), myHttpException2.message());
                    isMyHttpExc = true;
                } catch (Exception exc) {
                }
                if (!isMyHttpExc) {
                    //请求的地址不存在
                    int code = ((HttpException) e).code();
                    if (code == 504) {
                        netCallBack.onError(code, "网络异常，请检查您的网络状态");
                    } else if (code == 404) {
                        netCallBack.onError(code, "请求的地址不存在");
                    } else {
                        netCallBack.onError(code, "服务器异常");
                    }
                }
            } else if (e instanceof UnknownHostException) {
                netCallBack.onError(500, "域名解析失败");
            } else {
                netCallBack.onError(500, "error:" + e.getMessage());
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        } finally {
            Log.e(TAG, "error:" + e.getMessage());
        }
    }

    @Override
    public void onComplete() {
        Log.i(TAG, "onComplete.............................");
    }

}
